﻿Imports System.Xml.Linq
Imports System.Windows.Media.Imaging
Imports System.Collections.ObjectModel
Imports System.Windows.Browser

Partial Public Class MainPage
    Inherits UserControl

    ' todo: make option, store in cookie, isolated storage
    Private thumbRes As Integer = 60
    Private Photos As New ObservableCollection(Of Photo)
    Private Shared _baseUri As String

    Private _soapClient As PhotoService.PhotoServiceSoapClient
    Private ReadOnly Property SoapClient() As PhotoService.PhotoServiceSoapClient
        Get
            If _soapClient Is Nothing Then
                ' set up dynamic binding
                Dim binding As New System.ServiceModel.BasicHttpBinding
                Dim ep As New System.ServiceModel.EndpointAddress(App.baseUri & "/PhotoService.asmx")

                _soapClient = New PhotoService.PhotoServiceSoapClient(binding, ep)
                AddHandler _soapClient.GetTreeCompleted, AddressOf GotTree
                AddHandler _soapClient.GetFolderInfoCompleted, AddressOf GotFolderInfo
            End If
            Return _soapClient
        End Get
    End Property


    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        ShowWaiting(True)

        PhotoList.ItemsSource = Photos

        ' Start getting the folder structure

        SoapClient.GetTreeAsync()

    End Sub

    Private Sub GotTree(ByVal sender As Object, ByVal args As PhotoService.GetTreeCompletedEventArgs)
        Dim e = args.Result
        Dim ret As XElement = XElement.Parse(e)

        FillTree(daTree.Items, ret.Elements)
        ShowReady()
    End Sub

    Private Sub FillTree(ByVal itemCol As ItemCollection, ByVal datas As IEnumerable(Of XElement))
        Dim header As String
        For Each e In datas
            Dim tvi As New TreeViewItem
            itemCol.Add(tvi)
            header = e.FirstNode.ToString(SaveOptions.DisableFormatting)
            header = header.Substring(header.LastIndexOf("\") + 1)

            tvi.Header = header
            tvi.Tag = e

            If e.HasElements Then
                FillTree(tvi.Items, e.Elements)
            End If
        Next
    End Sub

    Private Sub daTree_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles daTree.SelectedItemChanged
        ShowWaiting()

        ' Clear the photos we're displaying now and retrieve for current folder
        splash.Visibility = Windows.Visibility.Collapsed
        Photos.Clear()

        Dim tvi As TreeViewItem = DirectCast(e.NewValue, TreeViewItem)
        Dim ele As XElement = DirectCast(tvi.Tag, XElement)
        Dim folder = ele.FirstNode.ToString(SaveOptions.DisableFormatting)

        SoapClient.GetFolderInfoAsync(folder)

    End Sub

    Private Sub GotFolderInfo(ByVal sender As Object, ByVal args As PhotoService.GetFolderInfoCompletedEventArgs)
        ShowReady()
        Dim e = args.Result
        Dim ret As XElement = XElement.Parse(e)

        For Each ele In ret.Elements
            If ele.Name.LocalName.Equals("image") Then
                Photos.Add(New Photo(ele))
            End If
        Next

    End Sub

    Private Sub wrapper_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        If TypeOf e.OriginalSource Is Image Then
            ShowFullImage(DirectCast(DirectCast(e.OriginalSource, Image).DataContext, Photo))
        End If
    End Sub

    Private Sub ShowFullImage(ByVal p As Photo)
        imageZoom.Show(Photos, Photos.IndexOf(p))
    End Sub

    Private Sub ShowWaiting(Optional ByVal suppressAnimate As Boolean = False)
        VisualStateManager.GoToState(Me, "Waiting", Not suppressAnimate)
    End Sub

    Private Sub ShowReady()
        VisualStateManager.GoToState(Me, "Ready", True)
    End Sub

    Private Sub StateGroup_CurrentStateChanged(ByVal sender As Object, ByVal e As System.Windows.VisualStateChangedEventArgs) Handles StateGroup.CurrentStateChanged
        Dim old = e.OldState

    End Sub
End Class

